package com.greenant.store.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.SaveCallback;
import com.greenant.store.R;
import com.greenant.store.bean.UserIdentification;
import com.greenant.store.utils.Const;
import com.greenant.store.utils.UtilTools;
import com.lsarah.utils.SystemBarTintManager;

public class MyAlipayChangeAccountActivity extends BaseActivity {
	private View mViewLayoutStep1;
	private EditText mEtStep1;
	private Button mBtnStep1;
	
	private View mViewLayoutStep2;
	private EditText mEtStep2;
	private Button mBtnStep2;
	
	private UserIdentification mUserIdentification;
	private boolean isCommiting = false;
	
	private TextWatcher mTextWatcherStep1 = new TextWatcher() {
		@Override
		public void onTextChanged(CharSequence s, int start, int before, int count) {
			String str = mEtStep1.getText().toString();
			if(str != null && str.length() >= 15) {
				mBtnStep1.setEnabled(true);
				mBtnStep1.setTextColor(getResources().getColor(R.color.firebrick));
			} else {
				mBtnStep1.setEnabled(false);
				mBtnStep1.setTextColor(getResources().getColor(android.R.color.darker_gray));
			}
		}
		
		@Override
		public void beforeTextChanged(CharSequence s, int start, int count, int after) {
		}
		
		@Override
		public void afterTextChanged(Editable s) {
		}
	};
	
	private TextWatcher mTextWatcherStep2 = new TextWatcher() {
		@Override
		public void onTextChanged(CharSequence s, int start, int before, int count) {
			String str = mEtStep2.getText().toString();
			if(str != null && str.length() > 0) {
				mBtnStep2.setEnabled(true);
				mBtnStep2.setTextColor(getResources().getColor(R.color.firebrick));
			} else {
				mBtnStep2.setEnabled(false);
				mBtnStep2.setTextColor(getResources().getColor(android.R.color.darker_gray));
			}
		}
		
		@Override
		public void beforeTextChanged(CharSequence s, int start, int count, int after) {
		}
		
		@Override
		public void afterTextChanged(Editable s) {
		}
	};
	
	private OnClickListener mOnClickListener1 = new OnClickListener() {
		@Override
		public void onClick(View v) {
			String recordIdNum = mUserIdentification.getIDNum();
			String idNum = mEtStep1.getText().toString();
			if(recordIdNum != null && recordIdNum.equals(idNum)) {
				mViewLayoutStep1.setVisibility(View.GONE);
				mViewLayoutStep2.setVisibility(View.VISIBLE);
			} else {
				UtilTools.showMyToast(MyAlipayChangeAccountActivity.this, 
						getResources().getString(R.string.change_account_prompt_idnum_error));
			}
		}
	};
	
	private OnClickListener mOnClickListener2 = new OnClickListener() {
		@Override
		public void onClick(View v) {
			if(isCommiting) {
				return;
			}
			
			String newAccount = mEtStep2.getText().toString();
			if(TextUtils.isEmpty(newAccount)) {
				UtilTools.showMyToast(MyAlipayChangeAccountActivity.this, 
						getResources().getString(R.string.change_account_hint_account));
				return;
			}
			
			String recordAccount = mUserIdentification.getAccount();
			if(newAccount.equals(recordAccount)) {
				finish();
				return;
			}
			
			isCommiting = true;
			mUserIdentification.setAccount(newAccount);
			mUserIdentification.setAuthenticated(false);
			mUserIdentification.saveInBackground(new SaveCallback() {
				@Override
				public void done(AVException arg0) {
					isCommiting = false;
					if(arg0 == null) {
						UtilTools.showMyToast(MyAlipayChangeAccountActivity.this, 
								getResources().getString(R.string.change_account_prompt_change_suc));
						setResult(RESULT_OK);
						finish();
					} else {
						UtilTools.showMyToast(MyAlipayChangeAccountActivity.this, 
								getResources().getString(R.string.change_account_prompt_change_fai));
					}
				}
			});
		}
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		SystemBarTintManager.setContentView(this, R.layout.my_alipay_change_account_activity,
				getResources().getColor(R.color.firebrick));
		
		mUserIdentification = Const.mUserIdentification;
		if(mUserIdentification != null) {
			mViewLayoutStep1 = findViewById(R.id.change_account_step_1_layout);
			mEtStep1 = (EditText) findViewById(R.id.change_account_step_1_et);
			mBtnStep1 = (Button) findViewById(R.id.change_account_step_1_btn);
			mEtStep1.addTextChangedListener(mTextWatcherStep1);
			mBtnStep1.setOnClickListener(mOnClickListener1);
			mBtnStep1.setEnabled(false);
			
			mViewLayoutStep2 = findViewById(R.id.change_account_step_2_layout);
			mEtStep2 = (EditText) findViewById(R.id.change_account_step_2_et);
			mBtnStep2 = (Button) findViewById(R.id.change_account_step_2_btn);
			mEtStep2.addTextChangedListener(mTextWatcherStep2);
			mBtnStep2.setOnClickListener(mOnClickListener2);
			mBtnStep2.setEnabled(false);
		}
	}
	
	public void goBack(View view) {
		finish();
	}
}



